package com.iqiyi.pps.epg.core.service.statistic;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticOcrData;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticOcrDataPage;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.statistic.StatisticOcrDataDao;
import com.iqiyi.pps.epg.core.model.statistic.StatisticOcrData;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhang on 2016/1/25.
 */
@Service
@Transactional
public class StatisticOcrDataServiceImpl implements StatisticOcrDataService {

    @Autowired
    private StatisticOcrDataDao dao;

    @Override
    public TStatisticOcrDataPage getPage(TPage page, TFilter filter) {
        return null;
    }

    @Override
    public void save(StatisticOcrData entity) {
        if (entity == null)
            return;
        dao.save(entity);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public TStatisticOcrDataPage findTStatisticOcrDataPage(TPage page, TFilter filter) {
        Map<String, String> map = filter.getFilters();
        String tempPattern = "${PARAMS}";
        String totalPattern = "select COUNT(*)";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        String hql = tempPattern + " from StatisticOcrData";
        Page<StatisticOcrData> dbPage = TransUtil.buildPage(page);

        hql = TransUtil.buildQueryStringByPropertyFilter(dbPage, hql, filterList);

        if (dbPage.isAutoCount()) {
            String totalCount = dao.getSession().createQuery(hql.replace(tempPattern, totalPattern)).uniqueResult().toString();
            page.setTotalCount(Long.valueOf(totalCount));
        }

        List result = dao.getSession().createQuery(hql.replace(tempPattern, ""))
                                .setMaxResults(dbPage.getPageSize())
                                .setFirstResult((dbPage.getPageNo() - 1) * dbPage.getPageSize())
                                .list();
        TStatisticOcrDataPage tStatisticOcrDataPage = new TStatisticOcrDataPage();
        tStatisticOcrDataPage.setTPage(page);
        List<TStatisticOcrData> tStatisticOcrDatas = new ArrayList<TStatisticOcrData>();
        for(Object obj : result) {
            StatisticOcrData statisticOcrData = (StatisticOcrData) obj;
            tStatisticOcrDatas.add(statisticOcrData.convertThriftModel());
        }
        tStatisticOcrDataPage.setResult(tStatisticOcrDatas);
        return tStatisticOcrDataPage;
    }


}
